<template>
    <view>
        <view v-if="listData.length > 0" >
            <view  v-for="(item,index) in listData" :key="index">
                <view class="bg_address" @click="selectAddress(item)">				
                    <view style="width: 80%">
                        <view>
                            <text class="name">{{item.name}}</text><text class="phone gray_font">{{item.phone}}</text>
                            <span class="defaultFlag" v-if="item.defaultFlag=='0'">默认</span>
                        </view>
                        <view style="font-size: 30rpx;margin-top: 15rpx;" class="gray_font">{{item.region}}{{item.address}}</view>
                    </view>
                    <view class="editView" v-if="deleteFlag">
                        <view class="editBtn"  @click.stop.prevent="goto('/pagesSub-My/my/addressDetails?id='+item.id)" >
                            <image   src="../static/images/edit.png"></image>
                        </view>
                        <view class="editBtn" style="margin-left:20rpx;"  @click="openDeletePop(item.id)" >
                            <image   src="../static/images/delete.png"></image>
                        </view>
                    </view>
                </view>
            </view>
        	<view class="btns"> 
				 <button @click="goto('/pagesSub-My/my/addressDetails')" class="arc_button btn_address" style="width:90%" type="primary">添加地址</button>
			</view>
        </view>
	
		<view v-if="listData.length == 0" class="btns">
			 <button @click="goto('/pagesSub-My/my/addressDetails')" class="arc_button btn_address" style="width: 90%;" type="primary">添加地址</button>
		</view>
        <!-- <view v-if="listData.length > 0">
            <view class="uni-list" v-for="(item,index) in listData" :key="index">
                <view>
                    <view class="uni-list-cell-pd" >姓名：{{item.name}}</view>
                    <view class="uni-list-cell-pd">手机号：{{item.phone}}</view>
                    <view class="uni-list-cell-pd">具体地址：{{item.address}}</view>
                </view>
                <button
                        @click="goto('/pages/my/addressDetails?id='+item.id)"
                        style="width: 250rpx"
                        type="primary">编辑地址</button>
                <button
                        @click="delAddress(item.id)"
                        style="width: 250rpx"
                        type="primary">删除地址</button>
            </view>
        </view> -->
       <!-- <view v-else>
            <button
                    @click="goto('/pages/my/addressDetails')"
                    type="primary">添加地址</button>
        </view> -->
<!-- 删除地址弹窗 -->
        <uni-popup ref="alertDialog" type="dialog">
            <view class="inputDialog">
                <view class="title"> 确定要删除此地址吗？ </view>
                <view class="button-bottom">
                    <view class="cancel" @click="dialogClose">取消</view>
                    <view @click="delAddress" class="green_words">确定</view>
                </view>
            </view>
        </uni-popup>
    </view>
</template>

<script>
    import { addressList,deleteAddress } from '@/api/prescribing.js';
    export default {
        data() {
            return {
                deleteFlag:true,
                addressId:'',//要删除的地址id
                listData: [],//列表数据

            }
        },
        onShow() {
            this.getAddressList();
             let pages = getCurrentPages();  //获取所有页面栈实例列表
                // let nowPage = pages[ pages.length - 1];  //当前页页面实例
                let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
                // prevPage.address= item;   //修改上一页data里面的name参数值为1211
                console.log( prevPage,'上个页面的地址')
                if(prevPage.route!='pages/my/index'){
                    this.deleteFlag = false;
                    
                }
                // console.log(this.deleteFlag,'标识99999---')

        },
        methods: {
              // 关闭删除地址弹窗
            dialogClose() {
                this.$refs.alertDialog.close();
                // this.doctorinfo()
            },
            // 打开删除地址弹窗
            openDeletePop(addressId){
                this.addressId = addressId;
                this.$refs.alertDialog.open();
            },
            // 待支付和立即支付跳转过来选中的地址
            selectAddress(item){
                let pages = getCurrentPages();  //获取所有页面栈实例列表
                let prevPage = pages[ pages.length - 2 ];  //上一页页面实例
                prevPage.address= item;   //修改上一页data里面的name参数值为1211
                // console.log(item, prevPage,nowPage,'选中的地址')
                if(prevPage.route!='pages/my/index'){
                    uni.navigateBack({
                        delta: 1,
                    });
                }
            },
            goto(url) {
                uni.navigateTo({
                    url:url
                })
            },
            getAddressList() {
                uni.showLoading({title: '加载中'});
                addressList().then(res => {

                    this.listData =res.rows;
                    //console.log(this.listData);
                    uni.hideLoading();

                }).catch(res => {
                    uni.hideLoading();
                    console.log(res);
                });

            },
            delAddress(){
                uni.showLoading({title: '加载中'});
                deleteAddress(this.addressId).then(res => {
                    uni.hideLoading();
                    uni.showToast({
                        title: res.msg,
                        duration: 1000
                    });
                    this.dialogClose();  //关闭弹窗
                    setTimeout(this.getAddressList(), 1000);

                }).catch(res => {
                    uni.hideLoading();
                    console.log(res);
                });
            }
        }
    }
</script>

<style scoped lang="scss">
    @import '@/static/css/style.css';
    .uni-list {
        margin-bottom: 30rpx;
        background-color: #fff;
        position: relative;
        width: 100%;
        display: -webkit-box;
        display: flex;
        -webkit-box-orient: vertical;
        -webkit-box-direction: normal;
        flex-direction: column;
    }
    .uni-list-cell-pd {
        padding: 22rpx 30rpx;
    }
	/* 样式 */
	.bg_address{
		margin-top: 20rpx;
		background-color: #FFFFFF;
		padding: 20rpx 30rpx;
        display: flex;
	}
    .editView{
        display: flex;
        margin-left:80rpx;
    }
    .editBtn{
        width: 45rpx;
        height: 45rpx;
        margin: 16rpx 0 0;
        image {
            width: 100%;
            height: 100%;
        }
    }
	.name{
		font-size: 33rpx;
	}
	.phone{
		font-size: 29rpx;
		margin-left: 30rpx;
	}
    .defaultFlag{
        margin-left: 15rpx;
        font-size: 24rpx;
        color:#4CCCBF;
        padding:4rpx;
        border:1px solid #4CCCBF;
        border-radius: 5rpx;
    }
	.btns{
		display: flex;
		justify-content: space-between;
		margin-top: 100rpx;
	}
	.arc_button{
		width: 45%;
	}
	.del_btn{
		color: #4CCCBF;
		border: 2rpx solid #4CCCBF !important;
		background-color:#f4f4f4 !important;
	}
	.btn_address{
		background: -webkit-linear-gradient(#77E2A4, #4CCDBC);
		color: #FFFFFF;
	}
</style>
